package com.flute.framework.index.fieldbuilder;

import java.util.List;

import org.apache.lucene.document.Field;

import com.flute.framework.index.analysis.PayloadTokenizer;
import com.flute.framework.stringprocessor.IStringProcessor;
import com.flute.tools.data.DataRecord;
import com.flute.tools.util.StringUtil;

public class LuceneMultiScoreFieldBuilder extends LuceneScoreFieldBuilder {
	private IStringProcessor processor;
	private List<String> dataFields;

	@Override
	public Object buildField(DataRecord record) {
		ScoreField sf = new ScoreField();

		StringBuilder value = new StringBuilder("");
		if (dataFields != null) {
			for (String field : dataFields) {
				if (field != null) {
					String subvalue = record.getFieldValue(field);
					if (subvalue != null) {
						value = value.append(value).append(
								StringUtil.UNIQUE_STRING).append(
								StringUtil.getStandardString(subvalue));
					}
				}
			}
		}

		sf.setText(value.toString());
		String[] ss = processor.process(value.toString()).trim().split(
				String.valueOf(StringUtil.UNIQUE_STRING));

		for (String s : ss) {
			if (!StringUtil.isNullOrEmpty(s)) {
				sf.addString(s);
				sf.addScore(s, getDefaultScore(s, value.toString()));
			}
		}

		return new Field(docField, new PayloadTokenizer(sf));
	}

	@Override
	public List<String> getDataFields() {
		return this.dataFields;
	}

	@Override
	public void setDataFields(List<String> field) {
		this.dataFields = field;
	}
}
